<template>
  <div id="appds">
    <div>

      <el-card style="width: 500px;height: 300px;background-color: rgba(255,255,255,0.3);margin: 13% auto">
        <div style="margin: 20px;"></div>
        <el-form :model="user" ref="ruleForm" :rules="rules" label-width="10px" style="width: 400px;margin: 30px auto">
          <el-form-item prop="username">
            <el-input type="text"
                      placeholder="账号"
                      prefix-icon="el-icon-user"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password"
                      show-password
                      placeholder="密码"
                      prefix-icon="el-icon-lock"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>

            <el-row :gutter="20">
              <el-col :span="16">
                <el-input type="text" placeholder="验证码" prefix-icon="el-icon-circle-close"
                          v-model="verification" prop="code"></el-input>
              </el-col>
              <el-col :span="8">
                <el-button v-show="!showCode" @click="getCode">获取验证码</el-button>
                <img v-show="showCode" :src="codeImg" style="width: 110px;height: 32px;background: white" @click="getCode">
              </el-col>
            </el-row>

          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="longin('ruleForm')">登录</el-button>
            <el-button type="warning" @click="toReg">去注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<style>
#appds {
  background-image: url("/public/imgs/classroom1.jpg");
  text-align: center;
  overflow: hidden;
  position: absolute;
  height: 100%;
  width: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
</style>
<script>
export default {
  data() {
    return {
      showCode:false,
      labelPosition: 'right',
      verification: "",
      user: {
        username: '',
        password: ''
      },
      codes: "",
      options: {
        canvasId: 'mycanvas',
        width: 200,
        height: 50,
        txt: ''
      },
      codeImg:'',
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'},
          {pattern: /^([1-9A-Za-z]*)$/, message: '请输入以字母和数字组成的密码', trigger: 'blur'}
        ],
        code:[{ required: true, message: '验证码不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    toReg() {
      this.$router.push("/reg");
    },
    /*从后端获取验证码*/
    getCode(){
      let url = "http://localhost:9281/code";
    //
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.codeImg = jsonResult.data;
          console.log(this.codeImg);
          //this.code(this.options);
          this.showCode = true;
        } else {
          alert(jsonResult.message);
          this.code(this.options);
        }
      })


    },
    /*产生随机数字*/
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },

    randomColor(min, max) {
      var r = this.randomNum(min, max);
      var g = this.randomNum(min, max);
      var b = this.randomNum(min, max);
      return "rgb(" + r + "," + g + "," + b + ")";
    },

    code(options) {
      console.log(options)
      //this.produceCode(options);
      var canvas = document.getElementById(options.canvasId);
      canvas.width = options.width || 300;//画布的宽
      canvas.height = options.height || 150;//画布的高
      var ctx = canvas.getContext("2d");//创建一个canvas对象
      ctx.textBaseline = "middle";
      ctx.fillStyle = this.randomColor(60, 255);
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      for (var i = 0; i < options.txt.length; i++) {
        var txt = options.txt.charAt(i);
        ctx.font = '50px SimHei';
        ctx.fillStyle = this.randomColor(60, 180); /*随机生成字体颜色*/
        ctx.shadowOffsetY = this.randomNum(-3, 3);
        ctx.shadowBlur = this.randomNum(-3, 3);
        ctx.shadowColor = "rgba(0, 0, 0, 0.3)";
        var x = options.width / (options.txt.length + 1) * (i + 1);
        var y = options.height / 2;
        var deg = this.randomNum(-30, 30);
        /*设置旋转角度和坐标原点*/
        ctx.translate(x, y);
        ctx.rotate(deg * Math.PI / 180);
        ctx.fillText(txt, 0, 0);
        /*恢复旋转角度和坐标原点*/
        ctx.rotate(-deg * Math.PI / 180);
        ctx.translate(-x, -y);
      }
      for (let i = 0; i < this.randomNum(1, 4); i++) {
        ctx.strokeStyle = this.randomColor(40, 180);
        ctx.beginPath();
        ctx.moveTo(this.randomNum(0, options.width), this.randomNum(0, options.height));
        ctx.lineTo(this.randomNum(0, options.width), this.randomNum(0, options.height));
        ctx.stroke();
      }
      for (let i = 0; i < options.width / 6; i++) {
        ctx.fillStyle = this.randomColor(0, 255);
        ctx.beginPath();
        ctx.arc(this.randomNum(0, options.width), this.randomNum(0, options.height), 1, 0, 2 * Math.PI);
        ctx.fill();
      }
    },
    f() {

    },
    longin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("开始登录")
          console.log(this.verification+"------------"+this.options.txt)
            let url = "http://localhost:9281/login";

            let formData = this.qs.stringify(this.user)
            formData +="&code="+this.verification;
            this.axios.post(url, formData).then((response) => {
              console.log("11111")
              if (response.data.state == 20000) {
                this.$message({
                  showClose: true,
                  message: '登录成功',
                  type: 'success'
                });
                console.log(response.data);
                localStorage.setItem("localJwt", response.data.jwt);
                let localUser = JSON.stringify(response.data.data)

                localStorage.setItem("localUser", localUser);
                console.log(response.data.data.menuInfo)
                let b = response.data.data.menuInfo;
                this.$router.push(b);
              } else {
                alert(response.data.message);
                this.getCode();
              }
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },

  },
  mounted() {
    //this.code(this.options);
  }
}
</script>
